<template>
  <div class="app-container">
    <el-form :inline="true" ref="refForm" :model="form">
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username" placeholder="请输入账号" @keyup.enter.native="submitFilterForm"></el-input>
      </el-form-item>
      <el-form-item label="获奖时间" prop="time">
        <el-date-picker
          v-model="form.time"
          type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="粽子类型" prop="zongziType">
        <el-select v-model="form.zongziType" placeholder="请选择粽子类型">
          <el-option label="全部" key="" value=""></el-option>
          <el-option label="如意粽子" key="0" value='0'></el-option>
          <el-option label="吉祥粽子" key="1" value='1'></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="奖品" prop="level">
        <el-select v-model="form.rewardId" placeholder="请选择奖品" :disabled="rewardDisableFlag">
          <el-option label="全部" value=""></el-option>
          <el-option v-for="(item,index) in rewardList" :label="item.name" :key="'rewardList'+index" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
        <el-button @click="selfResetForm('refForm')">重置</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-download" @click="exportHandle">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      :data="tableData.list"
      v-loading="loading"
      element-loading-text="加载中..."
      class="el-table-align-center">
      <el-table-column prop="username" label="账户名" align="center"></el-table-column>
      <el-table-column prop="zongziType" label="粽子类型" align="center">
        <template slot-scope="{row}">
          {{row.zongziType===0?'如意粽子':'吉祥粽子'}}
        </template>
      </el-table-column>
      <el-table-column prop="reward" label="获得奖品" align="center"></el-table-column>
      <el-table-column prop="createTime" label="中奖时间" align="center">
        <template slot-scope="{row}">
          {{row.createTime | toTime('YYYY-MM-DD HH:mm:ss')}}
        </template>
      </el-table-column>
      <el-table-column prop="realName" label="收货人姓名" align="center"></el-table-column>
      <el-table-column prop="phoneNum" label="收货人电话" align="center"></el-table-column>
      <el-table-column prop="address" label="所在地区" align="center"></el-table-column>
      <el-table-column prop="addressDetail" label="详细地址" align="center"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center"></el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { grid } from '@/build';
import { getRiceDumplingReceiveInfo } from '@/services/api/activity';
import rewardList from './reward';

export default {
  name: 'receiveInfo',
  components: {},
  mixins: [grid],
  data() {
    const time = [moment().format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')];
    const startTime = time[0];
    const endTime = time[1];
    return {
      tableData: {
        list: [],
        total: 0,
      },
      form: {
        time,
        username: '',
        zongziType: '',
        rewardId: '',
      },
      filters: {
        startTime,
        endTime,
        username: '',
        zongziType: '',
        rewardId: '',
        size: 20,
        page: 1,
      },
      rewardList: [],
      operateDialogFlag: false,
      rewardDisableFlag: true,
      rowData: undefined,
    };
  },
  mounted() {
    this.bindGet(getRiceDumplingReceiveInfo, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
      format: data => this.handleData(data),
      beforeSend: (data) => {
        delete data.time;
        return data;
      },
    });
    this.getList();
  },
  watch: {
    'form.zongziType': function (val) {
      this.form.rewardId = '';
      if (val === '') {
        this.rewardDisableFlag = true;
      } else {
        this.rewardDisableFlag = false;
        this.rewardList = _.filter(rewardList[val], ['rewardType', 0]);
      }
    },
  },
  methods: {
    handleData(data) {
      _.map(data.list, (item) => {
        let userInfo;
        if (item.userInfo !== '' && typeof item.userInfo === 'string') {
          userInfo = JSON.parse(item.userInfo).userInfo;
          item.realName = userInfo.username;
          item.phoneNum = userInfo.phone;
          item.remark = userInfo.remark;
          item.address = userInfo.address;
          item.addressDetail = userInfo.addressDetail;
        }
      });
      return data;
    },
    exportHandle() {
      const {
        startTime,
        endTime,
        username,
        zongziType,
        rewardId,
        size,
        page,
      } = this.filters;
      const ajaxData = {
        startTime,
        endTime,
        username,
        zongziType,
        rewardId,
        size,
        page,
      };
      const filename = '收货信息表.csv';
      const header = [
        '账号名',
        '粽子类型',
        '获得奖品',
        '中奖时间',
        '收货人姓名',
        '收货人电话',
        '所在地区',
        '详细地址',
        '备注',
      ];
      const filterField = [
        'username',
        'zongziType',
        'reward',
        'createTime',
        'realName',
        'phoneNum',
        'address',
        'addressDetail',
        'remark',
      ];
      getRiceDumplingReceiveInfo(ajaxData).then(([result, data]) => {
        if (result) {
          const exportData = this.responseFormatter(this.handleData(data).list);
          this.export2Excel(header, filterField, exportData, filename);
        } else {
          this.$message({ message: `导出失败,${data.message}`, type: 'warning' });
        }
      });
    },
    responseFormatter(data) {
      data.map((item) => {
        item.zongziType = item.zongziType === 0 ? '如意粽子' : '吉祥粽子';
        item.createTime = _.toTime(item.createTime, 'YYYY-MM-DD HH:mm:ss');
      });
      return data;
    },
    getList() {
      this.doGet({ action: 'getGrid' });
    },
    submitFilterForm() {
      const { time } = this.form;
      let startTime;
      let endTime;
      if (!_.isUndefined(time) && _.isArray(time)) {
        startTime = time[0];
        endTime = time[1];
      }
      this.filters = {
        ...this.filters, ...this.form, startTime, endTime,
      };
      this.gridSearch();
    },
    selfResetForm(formName) {
      this.resetForm(formName);
      this.submitFilterForm();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
